package cxydmmszl.chapter01.t002;

import java.util.Stack;

/**
 * @author habitplus
 * @since 2021-08-04 21:58
 */
public class MyQueue {
    private final Stack<Integer> pushStack = new Stack<>();
    private final Stack<Integer> popStack = new Stack<>();

    /**
     * 入队
     */
    public void offer(int num) {
        pushStack.push(num);
    }

    /**
     * 出队
     */
    public int poll() {
        ensureQueueNonEmpty();
        if (popStack.isEmpty()) {
            throw new RuntimeException("The queue is empty!");
        }
        return popStack.pop();
    }

    private void ensureQueueNonEmpty() {
        if (popStack.isEmpty()) {
            while (!pushStack.isEmpty()) {
                popStack.push(pushStack.pop());
            }
        }
    }
}
